home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / quopri.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  6.5 KB  |  283 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Conversions to/from quoted-printable transport encoding as per RFC 1521.'''
  5. __all__ = [
  6.     'encode',
  7.     'decode',
  8.     'encodestring',
  9.     'decodestring']
  10. ESCAPE = '='
  11. MAXLINESIZE = 76
  12. HEX = '0123456789ABCDEF'
  13. EMPTYSTRING = ''
  14.  
  15. try:
  16.     from binascii import a2b_qp, b2a_qp
  17. except ImportError:
  18.     a2b_qp = None
  19.     b2a_qp = None
  20.  
  21.  
  22. def needsquoting(c, quotetabs, header):
  23.     """Decide whether a particular character needs to be quoted.
  24.  
  25.     The 'quotetabs' flag indicates whether embedded tabs and spaces should be
  26.     quoted.  Note that line-ending tabs and spaces are always encoded, as per
  27.     RFC 1521.
  28.     """
  29.     if c in ' \t':
  30.         return quotetabs
  31.     if c == '_':
  32.         return header
  33.     if not c == ESCAPE:
  34.         pass
  35.     return not c == '_' if c <= c else c <= '~'
  36.  
  37.  
  38. def quote(c):
  39.     '''Quote a single character.'''
  40.     i = ord(c)
  41.     return ESCAPE + HEX[i // 16] + HEX[i % 16]
  42.  
  43.  
  44. def encode(input, output, quotetabs, header = 0):
  45.     """Read 'input', apply quoted-printable encoding, and write to 'output'.
  46.  
  47.     'input' and 'output' are files with readline() and write() methods.
  48.     The 'quotetabs' flag indicates whether embedded tabs and spaces should be
  49.     quoted.  Note that line-ending tabs and spaces are always encoded, as per
  50.     RFC 1521.
  51.     The 'header' flag indicates whether we are encoding spaces as _ as per
  52.     RFC 1522.
  53.     """
  54.     if b2a_qp is not None:
  55.         data = input.read()
  56.         odata = b2a_qp(data, quotetabs = quotetabs, header = header)
  57.         output.write(odata)
  58.         return None
  59.     
  60.     def write(s, output = output, lineEnd = '\n'):
  61.         if s and s[-1:] in ' \t':
  62.             output.write(s[:-1] + quote(s[-1]) + lineEnd)
  63.         elif s == '.':
  64.             output.write(quote(s) + lineEnd)
  65.         else:
  66.             output.write(s + lineEnd)
  67.  
  68.     prevline = None
  69.     while None:
  70.         line = input.readline()
  71.         if not line:
  72.             break
  73.         
  74.         outline = []
  75.         stripped = ''
  76.         if line[-1:] == '\n':
  77.             line = line[:-1]
  78.             stripped = '\n'
  79.         
  80.         for c in line:
  81.             if needsquoting(c, quotetabs, header):
  82.                 c = quote(c)
  83.             
  84.             if header and c == ' ':
  85.                 outline.append('_')
  86.                 continue
  87.             outline.append(c)
  88.         
  89.         if prevline is not None:
  90.             write(prevline)
  91.         
  92.         thisline = EMPTYSTRING.join(outline)
  93.         while len(thisline) > MAXLINESIZE:
  94.             write(thisline[:MAXLINESIZE - 1], lineEnd = '=\n')
  95.             thisline = thisline[MAXLINESIZE - 1:]
  96.         prevline = thisline
  97.         continue
  98.         if prevline is not None:
  99.             write(prevline, lineEnd = stripped)
  100.         
  101.  
  102.  
  103. def encodestring(s, quotetabs = 0, header = 0):
  104.     if b2a_qp is not None:
  105.         return b2a_qp(s, quotetabs = quotetabs, header = header)
  106.     StringIO = StringIO
  107.     import cStringIO
  108.     infp = StringIO(s)
  109.     outfp = StringIO()
  110.     encode(infp, outfp, quotetabs, header)
  111.     return outfp.getvalue()
  112.  
  113.  
  114. def decode(input, output, header = 0):
  115.     """Read 'input', apply quoted-printable decoding, and write to 'output'.
  116.     'input' and 'output' are files with readline() and write() methods.
  117.     If 'header' is true, decode underscore as space (per RFC 1522)."""
  118.     if a2b_qp is not None:
  119.         data = input.read()
  120.         odata = a2b_qp(data, header = header)
  121.         output.write(odata)
  122.         return None
  123.     new = ''
  124.     while None:
  125.         line = input.readline()
  126.         if not line:
  127.             break
  128.         
  129.         i = 0
  130.         n = len(line)
  131.         if n > 0 and line[n - 1] == '\n':
  132.             partial = 0
  133.             n = n - 1
  134.             while n > 0 and line[n - 1] in ' \t\r':
  135.                 n = n - 1
  136.         else:
  137.             partial = 1
  138.         while i < n:
  139.             c = line[i]
  140.             if c == '_' and header:
  141.                 new = new + ' '
  142.                 i = i + 1
  143.                 continue
  144.             if c != ESCAPE:
  145.                 new = new + c
  146.                 i = i + 1
  147.                 continue
  148.             if i + 1 == n and not partial:
  149.                 partial = 1
  150.                 break
  151.                 continue
  152.             if i + 1 < n and line[i + 1] == ESCAPE:
  153.                 new = new + ESCAPE
  154.                 i = i + 2
  155.                 continue
  156.             if i + 2 < n and ishex(line[i + 1]) and ishex(line[i + 2]):
  157.                 new = new + chr(unhex(line[i + 1:i + 3]))
  158.                 i = i + 3
  159.                 continue
  160.             new = new + c
  161.             i = i + 1
  162.         if not partial:
  163.             output.write(new + '\n')
  164.             new = ''
  165.             continue
  166.         continue
  167.         if new:
  168.             output.write(new)
  169.         
  170.  
  171.  
  172. def decodestring(s, header = 0):
  173.     if a2b_qp is not None:
  174.         return a2b_qp(s, header = header)
  175.     StringIO = StringIO
  176.     import cStringIO
  177.     infp = StringIO(s)
  178.     outfp = StringIO()
  179.     decode(infp, outfp, header = header)
  180.     return outfp.getvalue()
  181.  
  182.  
  183. def ishex(c):
  184.     """Return true if the character 'c' is a hexadecimal digit."""
  185.     if c <= c:
  186.         pass
  187.     elif not c <= '9':
  188.         if c <= c:
  189.             pass
  190.         elif not c <= 'f':
  191.             if c <= c:
  192.                 return c <= 'F'
  193.             c <= c
  194.     return c
  195.  
  196.  
  197. def unhex(s):
  198.     '''Get the integer value of a hexadecimal number.'''
  199.     bits = 0
  200.     for c in s:
  201.         if c <= c:
  202.             pass
  203.         elif c <= '9':
  204.             i = ord('0')
  205.         elif c <= c:
  206.             pass
  207.         elif c <= 'f':
  208.             i = ord('a') - 10
  209.         elif c <= c:
  210.             pass
  211.         elif c <= 'F':
  212.             i = ord('A') - 10
  213.         else:
  214.             break
  215.         bits = bits * 16 + (ord(c) - i)
  216.     
  217.     return bits
  218.  
  219.  
  220. def main():
  221.     import sys
  222.     import getopt
  223.     
  224.     try:
  225.         (opts, args) = getopt.getopt(sys.argv[1:], 'td')
  226.     except getopt.error:
  227.         msg = None
  228.         sys.stdout = sys.stderr
  229.         print msg
  230.         print 'usage: quopri [-t | -d] [file] ...'
  231.         print '-t: quote tabs'
  232.         print '-d: decode; default encode'
  233.         sys.exit(2)
  234.  
  235.     deco = 0
  236.     tabs = 0
  237.     for o, a in opts:
  238.         if o == '-t':
  239.             tabs = 1
  240.         
  241.         if o == '-d':
  242.             deco = 1
  243.             continue
  244.     
  245.     if tabs and deco:
  246.         sys.stdout = sys.stderr
  247.         print '-t and -d are mutually exclusive'
  248.         sys.exit(2)
  249.     
  250.     if not args:
  251.         args = [
  252.             '-']
  253.     
  254.     sts = 0
  255.     for file in args:
  256.         if file == '-':
  257.             fp = sys.stdin
  258.         else:
  259.             
  260.             try:
  261.                 fp = open(file)
  262.             except IOError:
  263.                 msg = None
  264.                 sys.stderr.write("%s: can't open (%s)\n" % (file, msg))
  265.                 sts = 1
  266.                 continue
  267.  
  268.         if deco:
  269.             decode(fp, sys.stdout)
  270.         else:
  271.             encode(fp, sys.stdout, tabs)
  272.         if fp is not sys.stdin:
  273.             fp.close()
  274.             continue
  275.     
  276.     if sts:
  277.         sys.exit(sts)
  278.     
  279.  
  280. if __name__ == '__main__':
  281.     main()
  282.  
  283.